package de.fhd.pms;

import java.util.Date;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

public class CopyOfServPmsBgTask extends HttpServlet {
	 private static final long serialVersionUID = 1L;
 	 
	private String greeting;
		
	public void init() throws ServletException {
	
		// greeting = getInitParameter("greeting");
		// ... z.B. test = new Thread(new MyClass()).run()
		new Thread(new Bg1Class()).run();
	  }
	}


class Bg1Class implements Runnable {
	private int _timeHours = 0;
	private int _timeMinutes = 0;
	private int _timeSeconds = 1;
	private int counter = 0;
	
	public void init(){
		
	}
	
	// ...
	public void run() {
	while (true) {
	sleepUntil(_timeHours, _timeMinutes, _timeSeconds);
	// my daily work goes here....
	this.counter ++;
	 // Bilder holen und verarbeiten
		}
	}
	
	static private void sleepUntil(int hours, int minutes, int seconds) {
		try {
		Thread.sleep(secondsUntil(hours, minutes, seconds) * 1000);
		} catch (InterruptedException ignore) { }
	}

	static private int secondsUntil(int hours, int minutes, int seconds) {
		Date d = new Date();
		int secondsToday = d.getHours() * 60 * 60 +
		d.getMinutes() * 60 + d.getSeconds();
		int secondsNext = hours * 60 * 60 + minutes * 60 + seconds;
		int result = secondsNext - secondsToday;
		if (result <= 0) { result += 24 * 60 * 60; }
		return result;
	}
	}

